/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function query() {
    var schema = "id,name,desc";
    var sdata = schema.split(",");
    var buf = new StringBuffer();
    for(var i = 0; i < sdata.length; i++) {
        buf.append("data[");
        buf.append(i);
        buf.append("]=obj.");
        buf.append(sdata[i]);
        buf.append(";");
    }
    var str_schema = buf.toString();
    var list = new List();
    list.add({
        id: "0000000001",
        name: "k001",
        desc: "desc001"
    });
        list.add({
        id: "0000000002",
        name: "k002",
        desc: "desc002"
    });
        list.add({
        id: "0000000003",
        name: "k003",
        desc: "desc003"
    });
            list.add({
        id: "0000000004",
        name: "k004",
        desc: "desc004"
    });
            list.add({
        id: "00000000005",
        name: "k005",
        desc: "desc005"
    });
    convert(list, str_schema);
}
function setTableHeader(tableId, columns, count, dataHead, link) {
    removeTableHead(tableId);
    removeTableFooter(tableId);
    insertTableHead(tableId, columns, tableId+"Header", count, dataHead, link);
    var i =0;
    var footer = new Array();
    for(i =0; i<columns.length;i++) {
        footer[i] = "&nbsp;";
    }
    insertTableFoot(tableId, footer);
}

function loadTable(schema, list, tableId, delfunc, edtfunc) {
    removeRowAll(tableId);
    var sdata = schema.split(",");
    var buf = new StringBuffer();
    for(var i = 0; i < sdata.length; i++) {
        buf.append("data[");
        buf.append(i);
        buf.append("]=obj.");
        buf.append(sdata[i]);
        buf.append(";");
    }
    var str_schema = buf.toString();
    convert(list, str_schema,tableId,delfunc,edtfunc);
}
function convert(list,schema,tableId, delfunc, edtfunc) {
    for(var i = 0; i < list.size(); i++) {
        var obj = list.get(i);
        var data = new Array();
        eval(schema);
        insertTableRow(data, tableId, delfunc, edtfunc);
    }
}
function getTable(tableId) {
    return document.getElementById(tableId);
}
function getTableBody(tableId) {
    var theTable = getTable(tableId);
    return theTable.tBodies[0];
}
function insertTableHead(tableId, THEADData, headName, count, dataHead, link) {
    //var THEADData = ['Hours','Minutes','Seconds','Milliseconds']
    //dataHead = "";
    var num_pages = dataHead.split(",", 200);
    var buf = new StringBuffer();
    buf.append("Total:"+ count);
    buf.append("&nbsp;&nbsp;");
    var i = 0;
    for(i=0; i< num_pages.length; i++) {
        var p = i + 1;
        buf.append("&nbsp;<a href=\"javaScript:void(0)\" onclick=\"");
        buf.append(link);
        buf.append("("+ num_pages[i]+")");
        buf.append("\">");
        buf.append(""+p);
        buf.append("</a>");
    }
    //alert(buf.toString());
    var theTable = getTable(tableId);
    var newCell;
    var newTHEAD = theTable.createTHead();
    newTHEAD.id = headName;
    var barrow = newTHEAD.insertRow(-1);
    newCell = barrow.insertCell(0);
    newCell.innerHTML = buf.toString();
    newTHEAD.rows[0].cells[0].setAttribute("colspan", THEADData.length);
    //newCell.setAttribute("colspan", THEADData.length);
    var newRow = newTHEAD.insertRow(-1);
    for (i = 0; i < THEADData.length; i++) {
        newCell = newRow.insertCell(i);
        newCell.innerHTML = THEADData[i];
        newTHEAD.rows[1].cells[i].setAttribute("class", "tableHead");
        //newTHEAD.rows[0].cells[i].className ="lookupTableHeader";
        //newTHEAD.rows[0].cells[i].style.backgroundColor="#999999";
        //newTHEAD.rows[0].cells[i].style.textAlign="center";
        //newTHEAD.rows[0].cells[i].style.fontStyle="oblique";
    }
}
function insertTableRow(nowData,tableId,delfunc,edifunc) {
    var theTableBody = getTableBody(tableId);
    var newCell;
    var rows = theTableBody.rows;
    var where = theTableBody.rows.length;
    var newRow = theTableBody.insertRow(where);
    newRow.style.height = "2px";
    var newCell1 = newRow.insertCell(0);
    newCell1.style.textAlign = "center";
    newCell1.innerHTML = "<img src='../imgs/delete16.png' align='center' onmouseover=\"icon_point(this)\" onclick=\""+delfunc+"('"+nowData[0]+"')\">";
    var newCell2 = newRow.insertCell(1);
    newCell2.style.textAlign = "center";
    newCell2.innerHTML = "<img src='../imgs/edit16.png' align='center' onmouseover=\"icon_point(this)\" onclick=\""+edifunc+"('"+nowData[0]+"')\">";
            if( (where % 2) == 1 ) {
            newCell1.style.backgroundColor ="#cccccc";
            newCell2.style.backgroundColor ="#cccccc";
        }
    for (var i = 0, j = 2; i < nowData.length; i++, j++) {
        newCell = newRow.insertCell(j);
        newCell.innerHTML = nowData[i];
        newCell.style.paddingRight = 5+"px";
        newCell.style.paddingLeft = 5+"px";
        //newCell.style.height = "2px";  
        if( (where % 2) == 1 ) {
            newCell.style.backgroundColor ="#cccccc";
        }
    }
}

function removeRow(tableId, index) {
    var theTableBody = getTableBody(tableId);
    theTableBody.deleteRow(index);
}
function removeRowAll(tableId) {
    var theTableBody = getTableBody(tableId);
    var rcount = theTableBody.rows.length;
    for (var i = 0; i < rcount; i++) {
        theTableBody.deleteRow(0);
    }
}
function removeTableHead(tableId) {
    document.getElementById(tableId).deleteTHead();
}
function removeTableFooter(tableId) {
    document.getElementById(tableId).deleteTFoot();
}
function insertTableFoot(tableId, TFOOTData) {
    //var TFOOTData = ['Hours','Minutes','Seconds','Milliseconds']
    var theTable = getTable(tableId);
    var newCell
    var newTFOOT = theTable.createTFoot()
    newTFOOT.id = tableId+"Footer";
    var newRow = newTFOOT.insertRow(-1)
    for (var i = 0; i < TFOOTData.length; i++) {
        newCell = newRow.insertCell(i);
        newCell.innerHTML = TFOOTData[i];
        newCell.setAttribute("class", "tableFoot");
    }
}
